package com.example.job.db;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.job.Model.JobModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.List;

import com.example.job.entity.Job;

public class jobdb extends SQLiteOpenHelper{
     private static final String DATABASE_NAME = "jobs.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "jobs";
    private static final String CREATE_TABLE_JOBS = "CREATE TABLE " + TABLE_NAME + " (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "type TEXT," +
            "title TEXT," +
            "company_title TEXT," +
            "min_salary INTEGER," +
            "max_salary INTEGER," +
            "experience TEXT," +
            "edu TEXT," +
            "tag TEXT," +
            "people TEXT," +
            "flag TEXT," +
            "welfare TEXT," +
            "img TEXT," +
            "city TEXT" +
            ");";

    public jobdb(Context context) {

         super(context, DATABASE_NAME, null, DATABASE_VERSION);
         Log.d("jobdb", "Database path: " + context.getDatabasePath(DATABASE_NAME).getAbsolutePath());
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_JOBS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public static List<Job> parseJson(InputStream inputStream) throws Exception {
        Gson gson = new Gson();
        Type listType = new TypeToken<List<Job>>(){}.getType();
        return gson.fromJson(new InputStreamReader(inputStream, "UTF-8"), listType);
    }

    //插入数据
    public static void insertJobs(Context context, List<Job> jobs) {
        jobdb dbHelper = new jobdb(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        for (Job job : jobs) {
            ContentValues values = new ContentValues();
            values.put("id", job.getId());
            values.put("type", job.getType());
            values.put("title", job.getTitle());
            values.put("company_title", job.getConpanytitle());
            values.put("min_salary", job.getMinsalary());
            values.put("max_salary", job.getMaxsalary());
            values.put("experience", job.getExperience());
            values.put("edu", job.getEdu());
            values.put("tag", job.getTag());
            values.put("people", job.getPeople());
            values.put("flag", job.getFlag());
            values.put("welfare", job.getWelfare());
            values.put("img", job.getImg());
            values.put("city", job.getCity());
            db.insert("jobs", null, values);
        }

        db.close();
    }

    //查询数据
    @SuppressLint("Range")
    public JobModel getJobById(int id) {
        SQLiteDatabase db = getReadableDatabase();
        String[] projection = {
            "id",
            "type",
            "title",
            "company_title",
            "min_salary",
            "max_salary",
            "experience",
            "edu",
            "tag",
            "people",
            "flag",
            "welfare",
            "img",
            "city"
        };

        String selection = "id = ?";
        String[] selectionArgs = { String.valueOf(id) };

        Cursor cursor = db.query(
            TABLE_NAME,
            projection,
            selection,
            selectionArgs,
            null,
            null,
            null
        );

        JobModel job = null;
        if (cursor.moveToFirst()) {
            job = new JobModel(
                cursor.getInt(cursor.getColumnIndex("id")),
                cursor.getString(cursor.getColumnIndex("title")),
                cursor.getString(cursor.getColumnIndex("company_title")),
                cursor.getString(cursor.getColumnIndex("type")),
                cursor.getString(cursor.getColumnIndex("min_salary")),
                cursor.getString(cursor.getColumnIndex("max_salary")),
                cursor.getString(cursor.getColumnIndex("experience")),
                cursor.getString(cursor.getColumnIndex("edu")),
                cursor.getString(cursor.getColumnIndex("tag")),
                cursor.getString(cursor.getColumnIndex("people")),
                cursor.getString(cursor.getColumnIndex("flag")),
                cursor.getString(cursor.getColumnIndex("welfare")),
                cursor.getString(cursor.getColumnIndex("img")),
                cursor.getString(cursor.getColumnIndex("city"))
            );
        }
        cursor.close();
        return job;
    }
}